using System;
using System.Collections.Generic;
using System.Text;

namespace Watchdog.Engines {
    internal class MinuteTimer : System.Timers.Timer {

        System.Timers.Timer synchtimer;
        public new event System.Timers.ElapsedEventHandler Elapsed;

        public MinuteTimer ()
            : base ( 10000 ) {
            base.Enabled = false;
            base.AutoReset = true;
            synchtimer = new System.Timers.Timer ();
            synchtimer.AutoReset = false;
            synchtimer.Enabled = false;
            synchtimer.Elapsed += new System.Timers.ElapsedEventHandler ( synchtimer_Elapsed );
            base.Elapsed += new System.Timers.ElapsedEventHandler ( Ticker_Elapsed );
        }

        void Ticker_Elapsed ( object sender, System.Timers.ElapsedEventArgs e ) {
            if (this.Elapsed != null)
                this.Elapsed ( this, null );
        }

        void synchtimer_Elapsed ( object sender, System.Timers.ElapsedEventArgs e ) {
            base.Start ();
            if (this.Elapsed != null)
                this.Elapsed ( this, null );
        }

        public new void Start () {
            DateTime t = DateTime.Now.AddMinutes ( 1 );
            DateTime nextminute = new DateTime ( t.Year, t.Month, t.Day, t.Hour, t.Minute, 0 );
            TimeSpan gap = nextminute.Subtract ( DateTime.Now );
            synchtimer.Interval = gap.Ticks / TimeSpan.TicksPerMillisecond;
            synchtimer.Start ();
        }
        public new void Stop () {
            base.Stop ();
        }
    }
}
